package com.zcx.qshop.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.qshop.R;

/**
 * Created by Administrator on 2015/8/8 0008.
 */
public class TitleView extends LinearLayout implements View.OnClickListener{

    public enum Type{

        BACK_ONE, BACK_TWO, NAME_ONE, NAME_TWO, SCREEN, SEARCH_VIEW

    }

    public enum Style{

        FULL

    }

    private OnTitleItemClickListener onTitleItemClickListener;

    @BoundView(R.id.title_back_one) private View back_one;

    @BoundView(R.id.title_back_two) private View back_two;

    @BoundView(R.id.title_name_one) private TextView name_one;

    @BoundView(R.id.title_name_two) private TextView name_two;

    @BoundView(R.id.title_screen) private TextView screen;

    @BoundView(R.id.title_search_view) private ViewGroup search_view;

    @BoundView(R.id.title_search_view_keyword) private EditText search_view_keyword;

    @BoundView(R.id.title_search_view_search) private TextView search_view_search;

    public TitleView(Context context, AttributeSet attrs) {

        super(context, attrs);

        BoundViewHelper.boundView(this, LayoutInflater.from(context).inflate(R.layout.view_title, this));

    }

    public void showType(Type...types){

        for (int i = 0; i<types.length; i++){

            if(types[i] == Type.BACK_ONE){

                showView(back_one);

            }else if(types[i] == Type.BACK_TWO){

                showView(back_two);

            }else if(types[i] == Type.NAME_ONE){

                name_one.setVisibility(View.VISIBLE);

            }else if(types[i] == Type.NAME_TWO){

                name_two.setVisibility(View.VISIBLE);

            }else if(types[i] == Type.SCREEN){

                showView(screen);

            }else if(types[i] == Type.SEARCH_VIEW){

                search_view.setVisibility(View.VISIBLE);

                showView(search_view_search);

            }

        }

    }

    public void showStyle(Style style){

        if(style == Style.FULL){

            setBackgroundResource(R.drawable.layer_solid_white_bottom_grey);

        }

    }

    private void showView(View view){

        view.setVisibility(View.VISIBLE);

        view.setOnClickListener(this);

    }

    public void setTitleName(String titleName){

        name_one.setText(titleName);

        name_two.setText(titleName);

    }

    public void setKeyword(String keyword){

        search_view_keyword.setText(keyword);

    }

    public void setOnTitleItemClickListener(OnTitleItemClickListener onTitleItemClickListener){

        this.onTitleItemClickListener = onTitleItemClickListener;

    }

    public OnTitleItemClickListener getOnTitleItemClickListener(){

        return onTitleItemClickListener;

    }

    @Override
    public void onClick(View v) {

        if(onTitleItemClickListener != null){

            switch (v.getId()){

                case R.id.title_back_one:
                case R.id.title_back_two:

                    onTitleItemClickListener.onBackClick();

                    break;

                case R.id.title_search_view_search:

                    onTitleItemClickListener.onSearchClick(search_view_keyword.getText().toString());

                    break;

                case R.id.title_screen:

                    onTitleItemClickListener.onScreenClick();

                    break;

            }

        }

    }

    @Override
    protected void onDetachedFromWindow() {

        super.onDetachedFromWindow();

    }

    public static class OnTitleItemClickListener{

        public void onBackClick(){}

        public void onSearchClick(String keyword){}

        public void onScreenClick(){}

    }

}
